/**
 * 2
 *  
 * @Author : Ruoyu Wang
 * @DateTime : 24-9-14 上午8:45
 */

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param prerequisites int整型vector<vector<>>
     * @param n int整型
     * @return int整型vector
     */
    vector<int> findOrder(vector<vector<int> > &prerequisites, int n) {
        vector<vector<int>> g(n);
        vector<int> inDegree(n, 0);

        for (auto p: prerequisites) {
            g[p[1]].push_back(p[0]);
            inDegree[p[0]]++;
        }

        vector<int> res;
        queue<int> q;
        for (int i = 0; i < n; ++i) {
            if (inDegree[i] == 0) {
                if (g[i].empty()) res.push_back(i);
                else q.push(i);
            }
        }

        while (!q.empty()) {
            int u = q.front();
            q.pop();
            res.push_back(u);
            for (auto x: g[u]) {
                if (inDegree[x] > 0) {
                    inDegree[x]--;
                    if (!inDegree[x]) q.push(x);
                }
            }
        }

        if (res.size() != n)return {};

        return res;
    }
};

int main() {

    return 0;
}